<?php
namespace app\question\controller\admin;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use app\question\model\QuestionCategory;
use app\question\model\QuestionContent;
use app\question\model\QuestionPaperMiddle;
use app\question\model\QuestionSubject;
use think\Request;

/**
 * @NodeAnnotation(
 *     title="题目管理",
 *     parent="question/admin.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1,
 * )
 */
class Questionlist extends Admin
{
    use ControllerCurd;
    public $model;
    public function initialize()
    {
        parent::initialize();
        $this->assign([
            "paperId" => $this->request->param("paper_id",0,'intval')
        ]);
        $this->model = new \app\question\model\QuestionList();
    }

    /**
     * @NodeAnnotation(
     *     title="题目列表",
     *     parent="question/admin.questionlist",
     *     MenuRoot="question/admin.index"
     * )
     */
    public function index($paper_id=0)
    {
        $where = [];
        if($paper_id) {
            $id = QuestionPaperMiddle::where('testpaper_id',$paper_id)->column('question_id');
            array_push($where,['id','in',$id]);
        }
        $this->assign([
            "questList" => $this->model->field('id,title')
                ->where($where)
                ->paginate()
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加题目分类",
     *     parent="question/admin.questionlist",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function addquest(Request $request)
    {
        if($request->isAjax())
        {
            $input = $request->post();
            $res = $this->model->create($input);
            if($res)
            {
                $jumpParam = [];
                if(isset($input['paper_id']) && !empty($input['paper_id']))
                {
                    QuestionPaperMiddle::create([
                        'testpaper_id' => $input['paper_id'],
                        "question_id"  => $res['id']
                    ]);
                    $jumpParam['paper_id'] = $input['paper_id'];
                }
                $this->success('添加成功',url('question/admin.questionlist/index',$jumpParam));
            }else{
                $this->error("添加失败");
            }
        }
        $this->assign([
            "cateList"    => (new QuestionCategory())->select(),
            "subjectList" => (new QuestionSubject())->select(),
            "level"       => $request->param("level","0",'intval')
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑题目",
     *     parent="question/admin.questionlist",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function editquest(Request $request,$id=0)
    {
        $this->assign([
            "quest" =>  $this->model->findData($id),
            "cateList"    => (new QuestionCategory())->select(),
            "subjectList" => (new QuestionSubject())->select()
        ]);
        $this->edit();
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除题目",
     *     parent="question/admin.questionlist",
     *     MenuRoot="question/admin.index",
     *     is_menu = 0
     * )
     */
    public function delquest()
    {
        $this->del('',function($model,$data){
            QuestionPaperMiddle::whereIn('question_id',$data)
                ->where('testpaper_id',$this->request->param('paper_id',0,'intval'))
                ->delete();
        });
    }
}